package com.touch.jgloo.core;
import java.awt.Color;

import javax.media.opengl.GL;

import com.sun.opengl.util.GLUT;
/**
 * GLUTStrokeText draws text to the screen by utilizing GLUT instead of using textures.
 * This should run better than using the Textured text, however it lacks customization and 
 * it might not work on some versions. Note: This has only been tested with Ortho view.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r461
 * @see com/touch/Licence 
 */
public class GLUTStrokeText extends JGLTextObject
{
	//GLUT Instance, only set when necessary
	private static GLUT GLUT_INSTANCE;
	/**
	 * Font size
	 * @since r461 
	 */
	public int size = 10;
	/**
	 * Font to use. Only supports GLUT.STROKE_MONO_ROMAN and GLUT.STROKE_ROMAN
	 * @since r461
	 */
	public static int FONT = GLUT.STROKE_MONO_ROMAN;
	/**
	 * Constructs a new GLUTStrokeText object without any specified caption.
	 * @since r461
	 */
	public GLUTStrokeText()
	{
		//confirm that a GLUT instance was made,
		if(GLUT_INSTANCE == null)
			//if not, make it
			GLUT_INSTANCE = new GLUT();
	}
	public GLUTStrokeText(final String caption)
	{
		//confirm that a GLUT instance was made,
		if(GLUT_INSTANCE == null)
			//if not, make it
			GLUT_INSTANCE = new GLUT();
		this.setCaption(caption);
	}
	public void render() 
	{
		//push the matrix up one
		GL_INSTANCE.glPushMatrix();
			//get the text's color
			final Color c = this.getColor();
				//set the color using the GL Instance
			GL_INSTANCE.glColor4f(
				c.getRed() / 255f, 
				c.getGreen() / 255f, 
				c.getBlue() / 255f, 
				c.getAlpha() / 255f);
			//rotate and translate the text so that it is aligned correctly 
			GL_INSTANCE.glRotatef(180f, 1f, 0f, 0f);
			GL_INSTANCE.glTranslatef(0f, -(size + 1), 0f);
			//scale the text to the specified size
			GL_INSTANCE.glScalef(size/100f, size/100f, 0.0f);
			//draw the text
			this.renderStrokeString(GL_INSTANCE, FONT, getCaption());
		//push the matrix down one	
		GL_INSTANCE.glPopMatrix();		
	}
	private void renderStrokeString(final GL gl, final int font, final String string) 
	{
        // Render The Text
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            GLUT_INSTANCE.glutStrokeCharacter(font, c);
        }
    }
	public GLUTStrokeText clone()
	{
		final GLUTStrokeText  clone = (GLUTStrokeText) JGLTextObject.createTextObject(this.getCaption(), JGLTextType.STROKE);
		clone.size = this.size;		
		
		return clone;
	}
}
